package co.yixiang.utils;

import cn.hutool.core.util.IdUtil;
import co.yixiang.service.dto.OrderQueryCriteia;
import co.yixiang.service.dto.RefundApplyCriteia;
import co.yixiang.service.dto.ScanCodePay;
import com.alibaba.fastjson.JSONObject;
import com.sun.jmx.snmp.Timestamp;
import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 客如云工具类
 */
@Slf4j
public class GuestUtils {
    /**
     * @param
     * @return String
     * @throws NoSuchAlgorithmException
     * @Description: SHA256加密字符串--获取签名
     */
    public static String getSign(String sortedParams) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(sortedParams.getBytes());
        byte byteBuffer[] = messageDigest.digest();
        StringBuffer strHexString = new StringBuffer();
        for (int i = 0; i < byteBuffer.length; i++) {
            String hex = Integer.toHexString(0xff & byteBuffer[i]);
            if (hex.length() == 1) {
                strHexString.append('0');
            }
            strHexString.append(hex);
        }
        // 得到返回結果
        String SHA256Sign = strHexString.toString();
        return SHA256Sign;
    }

    /**
     * type 为-1 表示获取昨天的日期，-2为前天的日期，以此类推。0 为今天的日期
     *
     * @param type
     * @return
     */
    public static Long strToDate(int type) {
        Long time = null;
        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.DATE, type);
        String tarday = new SimpleDateFormat("yyyy-MM-dd").format(cale.getTime()) + " 00:00:00";
        // String转Date
        try {
            date = format2.parse(tarday);
            time = date.getTime()/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 普通接口加密，获取到token之后
     **/
    public static void main(String[] args) throws NoSuchAlgorithmException {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("appKey", "fc08fa9d662091fec185dcdec90bc5eb");
        params.put("shopIdenty", "810814549");
        params.put("timestamp", OrderUtil.getSecondTimestampTwo());
        params.put("version", "1.0");
        StringBuilder sortedParams = new StringBuilder();
        params.entrySet().stream().forEachOrdered(paramEntry -> sortedParams.append(paramEntry.getKey()).append(paramEntry.getValue()));
        //请替换成真实的客如云token
        sortedParams.append("883a77a802db533c692459b234d658d0");
        //生成签名
        String sign = GuestUtils.getSign(sortedParams.toString());
        params.put("sign", sign);
        /********************商户通过该接口生成支付二维码begin********************/
//        String url= HttpsUtil.spellUrl("https://openapi.keruyun.com/open/v1/wallet/scanCode",params);
//        ScanCodePay pay=new ScanCodePay();
//        pay.setOutTradeNo(IdUtil.simpleUUID());
//        pay.setPayFee((long)1);
//        pay.setPayOrg("ORG_WEIXINPAY");
//        pay.setProductDesc("测试");
//        String pushParams=JSONObject.toJSONString(pay);
//        String reParams=HttpsUtil.post(url,pushParams);
//        System.out.println("请求url: "+url);
//        System.out.println("请求参数: "+pushParams);
//        System.out.println("响应参数: "+reParams);
        /********************商户通过该接口生成支付二维码end********************/

        /********************查询支付结果begin********************/
//        Map<String, String> mapParams = new HashMap<>();
//        mapParams.put("outTradeNo", "9f267f1006cd44d7801939f595811b1c");
//        String url = HttpsUtil.spellUrl("https://openapi.keruyun.com/open/v1/wallet/payQuery", params);
//        String pushParams = JSONObject.toJSONString(mapParams);
//        String reParams = HttpsUtil.post(url, pushParams);
//        System.out.println("请求url: " + url);
//        System.out.println("请求参数: " + pushParams);
//        System.out.println("响应参数: " + reParams);
        /********************查询支付结果end********************/

        /********************订单列表begin********************/
//        OrderQueryCriteia criteia = new OrderQueryCriteia();
//        criteia.setShopIdenty("810814549");
//        criteia.setStartTime(strToDate(-13));
//        criteia.setEndTime(strToDate(-1));
//        criteia.setTimeType(2);
//        String url = HttpsUtil.spellUrl("https://openapi.keruyun.com/open/v1/data/order/export2", params);
//        String pushParams = JSONObject.toJSONString(criteia);
//        String reParams = HttpsUtil.post(url, pushParams);
//        System.out.println("请求url: " + url);
//        System.out.println("请求参数: " + pushParams);
//        System.out.println("响应参数: " + reParams);
        /********************订单列表end********************/

        /********************退款申请begin********************/
//        RefundApplyCriteia criteia=new RefundApplyCriteia();
//        criteia.setRefundFee(1L);
//        criteia.setOutRefundNo("1291983839836504064");
//        criteia.setOutTradeNo("9f267f1006cd44d7801939f595811b1c");
//        String url = HttpsUtil.spellUrl("https://openapi.keruyun.com/open/v1/wallet/refundApply", params);
//        String pushParams = JSONObject.toJSONString(criteia);
//        String reParams = HttpsUtil.post(url, pushParams);
//        System.out.println("请求url: " + url);
//        System.out.println("请求参数: " + pushParams);
//        System.out.println("响应参数: " + reParams);
        /********************退款申请end********************/

        /********************退款状态查询begin********************/
        Map<String,String> mapParams=new LinkedHashMap<>();
        //调用方退款单号
        params.put("outRefundNo","1291983839836504064");
        //支付单号
        params.put("outTradeNo","9f267f1006cd44d7801939f595811b1c");
        String url = HttpsUtil.spellUrl("https://openapi.keruyun.com/open/v1/wallet/refundQuery", params);
        String pushParams = JSONObject.toJSONString(params);
        String reParams = HttpsUtil.post(url, pushParams);
        System.out.println("请求url: " + url);
        System.out.println("请求参数: " + pushParams);
        System.out.println("响应参数: " + reParams);
        /********************退款状态查询end********************/
    }
}
